package com.uk;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.Parameters;
import com.uk.data.entities.User;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
import com.vaadin.terminal.gwt.server.WebApplicationContext;


public class FaturaUKApplication extends Application {
	
	//LoginLayout loginLayout;
	//MainLayout mainLayout;
	
	private LoginWindow loginWindow;
	private MainWindow mainWindow;
	private IFaturaBean faturaBean;
	public static final Map<String, String> PARAMS = new HashMap<String, String>();
	private User loggedUser;
	
	
	public FaturaUKApplication(IFaturaBean faturaBean){
		this.faturaBean = faturaBean;
	}
	
	public void test(){
		System.out.println("OKOKOKOKOKOKOKOOK");
	}
	
	
	@Override
	public void init() {
		
		
		
		setTheme("myTheme");
		
		List<Parameters> parameters = faturaBean.getParameters();
		for (Parameters parameter : parameters) {
			PARAMS.put(parameter.getParam().name(), parameter.getValue());
		}
		
		
		this.loginWindow = new LoginWindow("Fatura - Ujesjelles Kanalizime", faturaBean);
		this.setMainWindow(this.loginWindow);
		
	
		//WebApplicationContext webCtx = (WebApplicationContext)  this.getContext();
		//HttpSession session = webCtx.getHttpSession();
		//session.setMaxInactiveInterval(120);
		
		//final SessionGuard sessionGuard = new SessionGuard();
	   // sessionGuard.setTimeoutWarningPeriod(1);
	    ///this.getMainWindow().addComponent(sessionGuard);
	    //sessionGuard.setTimeoutWarningXHTML("Sesioni juaj do te perfundoje pas _ minutash. <br/> Duhet te nderveproni me programin per ta mbajtur sesion aktiv.");
	    
		
	}
	
	public void loadMainLayout(){
		if (this.mainWindow == null) this.mainWindow = new MainWindow("Fatura - Ujesjelles Kanalizime", faturaBean);
		this.removeWindow(loginWindow);
		this.loginWindow = null;
		this.setMainWindow(mainWindow);
		
	}
	
	public void loadLoginLayout(){
		if (this.loginWindow == null) this.loginWindow = new LoginWindow("Fatura - Ujesjelles Kanalizime", faturaBean);
		this.removeWindow(this.mainWindow);
		this.mainWindow = null;
		this.setMainWindow(this.loginWindow);

	}

	@Override
	public void close() {
		super.close();
	}

	
	public static SystemMessages getSystemMessages() {
		    CustomizedSystemMessages m = new CustomizedSystemMessages();
		    m.setSessionExpiredCaption("Per arsye mos nderveprimi me programin, sesioni juaj eshte mbyllur");
		    m.setSessionExpiredMessage("");
		    return m;
		}
	
	
	
    @WebServlet(urlPatterns = "/*")
    public static class Servlet extends AbstractApplicationServlet {

    	@EJB
    	private IFaturaBean faturaBean;

    	@Override
    	protected Class<? extends Application> getApplicationClass() {
    		return FaturaUKApplication.class;
    	}

    	@Override
    	protected Application getNewApplication(HttpServletRequest request)
    	throws ServletException { return new FaturaUKApplication(faturaBean); }
    }



	public void setLoggedUser(User loggedUser) {
		this.loggedUser = loggedUser;
		this.setUser(this.loggedUser);
	}

	public static Map<String, String> getParams() {
		return PARAMS;
	}
    
	
}
